package com.ruoyi.web.controller.system;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.system.domain.RestaurantOrder;
import com.ruoyi.system.domain.RoomOrder;
import com.ruoyi.system.domain.RoomStatus;
import com.ruoyi.system.service.IRestaurantOrderService;
import com.ruoyi.system.service.IRoomOrderService;
import com.ruoyi.system.service.IRoomStatusService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @description: 类描述
 * @author: 强盛酒店
 * @createTime: 2023-03-12 22:20
 */
@RestController
@RequestMapping("/system/statistics")
public class StatisticsController {

    @Autowired
    private IRoomOrderService roomOrderService;
    SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd");
    @Autowired
    private IRoomStatusService roomStatusService;
    @Autowired
    private IRestaurantOrderService restaurantOrderService;

    @GetMapping("/count")
    public AjaxResult count() throws ParseException {
        String now = dateFormat.format(new Date());
        DateTime dateTime = DateUtil.offsetDay(new Date(), -6);
        String sevenDay = dateFormat.format(dateTime);
        List<String> everyDay = findEveryDay(sevenDay, now);
        Map<String,List<?>> map = new HashMap<>(16);
        List<String> dayList = new ArrayList<>();
        List<Double> moneyList = new ArrayList<>();
        for (String day : everyDay) {
            double price = 0;
            List<RestaurantOrder> orderList = restaurantOrderService.getData(day);
            if (!CollectionUtils.isEmpty(orderList)){
                for (RestaurantOrder restaurantOrder : orderList) {
                    price +=Double.parseDouble(restaurantOrder.getOrderPrice());
                }
            }
            dayList.add(day);
            moneyList.add(price);
        }
        map.put("day",dayList);
        map.put("money",moneyList);
        return AjaxResult.success(map);
    }

    @GetMapping("/get")
    public AjaxResult list() throws ParseException {
        String now = dateFormat.format(new Date());
        DateTime dateTime = DateUtil.offsetDay(new Date(), -6);
        String sevenDay = dateFormat.format(dateTime);
        List<String> everyDay = findEveryDay(sevenDay, now);
        Map<String,List<?>> map = new HashMap<>(16);
        List<String> dayList = new ArrayList<>();
        List<Integer> moneyList = new ArrayList<>();
        for (String day : everyDay) {
            int price = 0;
            List<RoomStatus> roomStatusList = roomStatusService.selectRoomStatusList(new RoomStatus() {{
                setOrderDate(day);
            }});
            if (!CollectionUtils.isEmpty(roomStatusList)){
                for (RoomStatus roomStatus : roomStatusList) {
                    RoomOrder roomOrder = roomOrderService.selectRoomOrderById(roomStatus.getOrderId());
                    price += Integer.parseInt(roomOrder.getOrderPrice())/roomOrder.getDwellingDay();
                }
            }
            dayList.add(day);
            moneyList.add(price);
        }
        map.put("day",dayList);
        map.put("money",moneyList);
        return AjaxResult.success(map);
    }


    /**
     * 传入两个时间范围，返回这两个时间范围内的所有日期，并保存在一个集合中
     * @param beginTime
     * @param endTime
     * @return
     * @throws ParseException
     */
    public static List<String> findEveryDay(String beginTime, String endTime)
            throws ParseException {
        //1.创建一个放所有日期的集合
        List<String> dates = new ArrayList<>();
        //2.创建时间解析对象规定解析格式
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

        //3.将传入的时间解析成Date类型,相当于格式化
        Date dBegin = sdf.parse(beginTime);
        Date dEnd = sdf.parse(endTime);
        //4.将格式化后的第一天添加进集合
        dates.add(sdf.format(dBegin));
        //5.使用本地的时区和区域获取日历
        Calendar calBegin = Calendar.getInstance();
        //6.传入起始时间将此日历设置为起始日历
        calBegin.setTime(dBegin);
        //8.判断结束日期是否在起始日历的日期之后
        while (dEnd.after(calBegin.getTime())) {
            // 9.根据日历的规则:月份中的每一天，为起始日历加一天
            calBegin.add(Calendar.DAY_OF_MONTH, 1);
            //10.得到的每一天就添加进集合
            dates.add(sdf.format(calBegin.getTime()));
            //11.如果当前的起始日历超过结束日期后,就结束循环
        }
        return dates;
    }
}
